<?php
namespace app\backstage\controller;
use think\Controller;
use think\Db;
use think\facade\Request;

class AdvertUser extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    /*广告Api接口设置*/
    public function api_setting()
    {
        $type = 'advert_api'.request()->_user->id;

        if( Request::isGet() ) {

            $data = Db::table('cps_setting')->where('type','=',$type)->value('data');
            $this->assign('info', json_decode($data,true));

            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');

        $advert_api['secret'] = $params['secret'];
        $advert_api['interface_domain'] = $params['interface_domain'];
        $advert_api['email'] = $params['email'];

        $data[] = ['type'=>$type,'data'=>json_encode($advert_api)];

        $Setting = \think\facade\App::model('Setting', 'logic');
        $result  = $Setting->editInfo($data);

        return json($result);
    }

    public function index()
    {
        $Account = \think\facade\App::model('Account', 'logic');
        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;

        $channels = $Account->getChannels($agentId);

        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    public function list_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Advert = \think\facade\App::model('AdvertUser', 'logic');
        $result = $Advert->getList($params);

        return sucJson($result['list'], $result['count']);
    }

    public function add()
    {
        if( Request::isGet() ){

            $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
            $Account = \think\facade\App::model('Account', 'logic');
            $channels = $Account->getChannels($agentId);

            $this->assign('advert_medias',advertMediaList());
            $this->assign('channels',$channels);
            $this->assign('role_name',request()->_role->alias);
            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'AdvertUser.add');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Advert = \think\facade\App::model('AdvertUser','logic');

        $record = Db::table('quick_channel_advert')
            ->where('advert_account',$params['advert_account'])
            ->field('id')->findOrEmpty();

        if($record){
            return json(['code'=>101,'msg'=>'该广告账户已绑定代理商，请勿重复添加']);
        }

        $data   = $Advert->dataProcessing($params);
        $result = $Advert->addInfo($data['data']);

        return json($result);
    }

    public function preview()
    {
        $params = Request::param('', '', 'strip_tags');

        $where[] = ['id','=',$params['id']];

        $Advert = \think\facade\App::model('AdvertUser','logic');
        $info = $Advert->getInfo($where);

        if(!$info){
            $this->error('该信息记录不存在');
        }

        $agentId = request()->_role->alias == 'agent' ? request()->_user->id : 0;
        $Account = \think\facade\App::model('Account', 'logic');
        $channels = $Account->getChannels($agentId);

        $this->assign('advert_medias',advertMediaList());
        $this->assign('channels',$channels);
        $this->assign('role_name',request()->_role->alias);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function edit()
    {
        $params = Request::post('','','strip_tags,trim');

        $verify_result = $this->validate($params, 'AdvertUser.edit');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Advert = \think\facade\App::model('AdvertUser','logic');

        $record = Db::table('quick_channel_advert')
            ->where('advert_account',$params['advert_account'])
            ->field('id')->findOrEmpty();

        if($record && $record['id'] != $params['id']){
            return json(['code'=>101,'msg'=>'该广告账户已绑定代理商，请勿重复添加']);
        }

        $where_u[] = ['id','=',$params['id']];

        if (request()->_role->alias == 'agent') {

            $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',request()->_user->id)
                ->column('id');

            if(!$channel_ids){
                return json(['code'=>101,'msg'=>'该记录未归属给该代理，请核实后操作']);
            }

            $where_u[] = ['channel_id', 'in', $channel_ids];
        }

        $data   = $Advert->dataProcessing($params);
        $result = $Advert->editInfo($where_u,$data['data']);

        return json($result);
    }

    /*数据删除*/
    public function delete()
    {
        $params = Request::param();
        $verify_result = $this->validate($params, 'AdvertUser.info');

        if(true !== $verify_result){
            $this->error($verify_result);
        }

        $Advert = \think\facade\App::model('AdvertUser','logic');

        $where[] = ['id','in',$params['id']];

        if (request()->_role->alias == 'agent') {

            $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',request()->_user->id)
                ->column('id');

            if(!$channel_ids){
                return json(['code'=>101,'msg'=>'该记录未归属给该代理，请核实后操作']);
            }

            $where[] = ['channel_id', 'in', $channel_ids];
        }

        $result  = $Advert->delInfo($where);

        return json($result);
    }


}